import type { BreadcrumbItem } from '@/types/router'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useBreadcrumbStore = defineStore('breadcrumb', () => {
    const breadcrumbs = ref<BreadcrumbItem[]>([])

    function setBreadcrumbs(items: BreadcrumbItem[]) {
        breadcrumbs.value = items
    }

    function clearBreadcrumbs() {
        breadcrumbs.value = []
    }

    function updateDynamicBreadcrumb(index: number, title: string, to?: string) {
        if (index >= 0 && index < breadcrumbs.value.length) {
            breadcrumbs.value[index].title = title
            if (to) {
                breadcrumbs.value[index].to = to
            }
        }
    }

    return {
        breadcrumbs,
        setBreadcrumbs,
        clearBreadcrumbs,
        updateDynamicBreadcrumb,
    }
})
